package com.linkoog.devtools.dubbo.toolwindow.workbenche.left.list;

import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBList;
import com.intellij.util.ui.JBFont;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.DubboFavorite;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

public class DubboFavoritesJList extends JBList<DubboFavorite> {

    public DubboFavoritesJList(@NotNull ListModel<DubboFavorite> dataModel) {
        super(dataModel);
        setEmptyText("No dubbo favorite available");
        setCellRenderer(new DubboFavoritesRender());
    }


    private static class DubboFavoritesRender extends JPanel implements ListCellRenderer<DubboFavorite>{

        private final JLabel nameLabel = new JLabel();
        private final JLabel messageLabel = new JLabel();

        private JBColor selectionBackground = new JBColor(0xE9EEF5, 0x464A4D);
        private JBColor unfocusedSelectionBackground = new JBColor(0xF5F5F5, 0x464A4D);


        public DubboFavoritesRender() {
            setBorder(JBUI.Borders.empty(5, 6));
            setLayout(new MigLayout(new LC().gridGap("0", "0").insets("0", "0", "0", "0").fillX()));

            String gapAfter = JBUI.scale(5) +"px";

            nameLabel.setFont(JBFont.label().biggerOn(2).asBold());
            add(nameLabel, new CC().minWidth("pref/2px").growX().pushX().wrap());
            add(messageLabel,new CC().minWidth("pref/2px").wrap());
        }

        @Override
        public Component getListCellRendererComponent(JList<? extends DubboFavorite> jList, DubboFavorite dubboFavorite, int index, boolean isSelected, boolean cellHasFocus) {
            UIUtil.setBackgroundRecursively(this, background(jList, isSelected, jList.hasFocus()));
            Color primaryTextColor = foreground(isSelected, jList.hasFocus());
            Color secondaryTextColor = secondaryForeground(jList, isSelected);

            nameLabel.setText(dubboFavorite.getName());
            nameLabel.setForeground(primaryTextColor);

            messageLabel.setText(dubboFavorite.getInface() + "#" + dubboFavorite.getMethodName());
            messageLabel.setForeground(secondaryTextColor);
            return this;
        }

        private Color background(JList<?> list, boolean isSelected, boolean hasFocus) {
            if (isSelected){
                return hasFocus?
                        JBColor.namedColor("Table.lightSelectionBackground", selectionBackground)
                        : JBColor.namedColor("Table.lightSelectionInactiveBackground", unfocusedSelectionBackground);
            }
            return  list.getBackground();
        }

        private Color foreground(boolean isSelected, boolean hasFocus){
            Color defaultColor = UIUtil.getListForeground();
            if (isSelected) {
                return hasFocus? JBColor.namedColor("Table.lightSelectionForeground", defaultColor)
                        : JBColor.namedColor("Table.lightSelectionInactiveForeground", defaultColor);
            } else{
                return JBColor.namedColor("Table.foreground", defaultColor);
            }
        }

        private Color secondaryForeground(JList<?> list,boolean isSelected){
			if (isSelected) {
                return foreground(true, list.hasFocus());
            } else {
                return JBColor.namedColor("Component.infoForeground", UIUtil.getContextHelpForeground());
            }
        }
    }
}
